package _2022.hot100._1_两数之和;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

/**
 * @author： YHSimon
 * @date： 2022-03-20 16:31
 */
public class Solution {
    //暴力枚举
    public int[] twoSum1(int[] nums, int target) {
        for (int i = 0; i < nums.length; i++) {
            for (int j = i + 1; j < nums.length; j++) {
                if (nums[i] + nums[j] == target) {
                    return new int[]{i, j};
                }
            }
        }
        return new int[0];
    }


    //哈希表
    public int[] twoSum2(int[] nums, int target) {
        Map<Integer, Integer> map = new HashMap<>();
        for (int i = 0; i < nums.length; i++) {
            if (map.containsKey(target - nums[i])) {
                return new int[]{map.get(target - nums[i]), i};
            }
            map.put(nums[i], i);

        }
        return new int[0];
    }


    public static void main(String[] args) {
        Solution s = new Solution();
        int[] results = s.twoSum1(new int[]{2, 9, 7, 6}, 8);
//        int[] results = s.twoSum2(new int[]{2, 9, 7, 6}, 8);
        System.out.println(Arrays.toString(results));
    }
}
